<?php


namespace App\Service\System;


use App\Repositories\Settings\CodeRepository;
use Illuminate\Support\Arr;

class CodeService
{
    function get($code, $value = false)
    {
        if (strlen($code) == 2) {
            $rep = new CodeRepository();
            $values = $rep->getMap($code);
        } else {
            $values = __('codes.' . $code);
        }
        if (empty($value)) {
            return $values;
        } else {
            return Arr::get($values, $value);
        }
    }

    function text($code, $value)
    {
        $values = $this->get($code);
        if (empty($value)) {
            return '';
        }
        return Arr::get($values, $value, '');
    }

    /**
     * 获取键值
     * @param string $code
     * @param array $notKeys
     * @param bool|array $hasKeys
     * @return array
     */
    public function kv(string $code, array $notKeys = [], $hasKeys = true): array
    {
        if (strlen($code) == 2) {
            $rep = new CodeRepository();
            $kv = $rep->getMap($code);
        } else {
            $kv = __('codes.' . $code);
        }

        if (count($notKeys)) {
            foreach ($notKeys as $key) {
                unset($kv[$key]);
            }
        }
        if (is_array($hasKeys)) {
            foreach ($kv as $k => $v) {
                if (in_array($k, $hasKeys)) {
                    unset($kv[$k]);
                }
            }
        }
        return $kv;
    }
}